package bbms.Service;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bbms.util.StringUtil;

public class UnitTestBase {
	
	private String springXmlpath;
	private ClassPathXmlApplicationContext context;
	
	
	public UnitTestBase() {

	}

	public UnitTestBase(String springXmlpath) {
		super();
		this.springXmlpath = springXmlpath;
	}

	@Before
	public void before(){
		if(StringUtil.isEmpty(springXmlpath)){
			springXmlpath = "applicationContext.xml";
		}
		
		try{
			context = new ClassPathXmlApplicationContext(springXmlpath);
			context.start();
		} catch(Exception e){
			e.printStackTrace();
		}
			
	}
	
	@After
	public void destroy(){
		context.destroy();
	}
	
	@SuppressWarnings("unchecked")
	protected <T extends Object> T getBean(String beanId){
		return (T)context.getBean(beanId);
	}
	
	protected <T extends Object> T getBean(Class<T> clazz){
		return context.getBean(clazz);
	}
}
